<script setup>
import {onMounted, ref, reactive} from 'vue';
import {get, post} from '../js/http.js';
import help from '../components/help.vue'
import indexConfigModal from '../components/modal/index_config_modal.vue'
import indexFieldModal from '../components/modal/index_filed_modal.vue'
import icon from '../components/icon.vue'
import pageHead from '../components/pagehead.vue'
import nodata from '../components/nodata.vue'

let indexRoot = ref("./lucene")
let settingData = ref([])
const isEdit = ref(false)
const activeTab = ref(0)
let currentIndex = ref({})
let dbInfo = ref({})
let showHelp = ref(false)


onMounted(() => {
  getSetting();
})

function getSetting() {
  get("/admin/setting/getSetting").then(res => {
    settingData.value = res.result.index
  }).catch(error => console.log(error))
}

async function openModel(item, type) {
  currentIndex.value = item
  activeTab.value = type
  isEdit.value = true
}


async function getRuntimeConfig() {
  let url = "/admin/setting/getRuntimeConfig";
  get(url).then(res => {
  }).catch(error => console.log(error))
}

function addHandle() {
  let newIndex = { name: "NewIndex", fields: "*", analyzer: "smartcn",fsdType:'nio', type: "normal", cdc: false, path: indexRoot.value, fieldsConfig: [] }
  currentIndex.value = newIndex;
  isEdit.value = true
  activeTab.value = 0
}

function deleteHandle(item, itemIndex) {
  window.confirm("确认删除索引", () => {
  }, () => {
    if (item.id) {
      post("/admin/setting/deleteSetting", item).then(res => {
      }).catch(error => console.log(error))
    }
    settingData.value.splice(itemIndex, 1);
  })
}

async function saveHandle() {
  isEdit.value = ''
  let res = await post("/admin/setting/updateSetting", currentIndex.value)
  if (res.success) {
    window.message("保存成功", 'success')
    getSetting();
  } else {
    window.message(res.message, 'warning')
  }
}

function closeModel() {
  isEdit.value = false
}

function restartOneIndex(indexInfo) {
  window.confirm("确认是否重新加载配置", () => {
  }, () => {
    post("/admin/restartIndex", {name: indexInfo.name}).then(res => {
      if (res.success) {
        window.message("重启成功", 'success')
      } else {
        window.message(res.message, 'warning')
      }
    })
    .catch(error => window.message(error, 'warning'))
  })
}

</script>
<template>
  <pageHead title="索引设置" iconName="modular">
    <button class="button is-link is-outlined mr-3" @click="getSetting"><icon icon="refresh" class="is-link mr-1"></icon>刷新</button>
    <button class="button is-link is-outlined" @click="addHandle"><icon icon="add-bold" class="is-link mr-1"></icon>新增索引</button>
  </pageHead>

  <div v-if="settingData">
    <table class="table is-fullwidth">
      <thead>
      <tr>
        <th>备注</th><th>索引</th><th>数据库</th><th>数据表</th><th>类型</th><th>字段</th><th>关键字</th><th style="text-align: center">操作</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="(item,itemIndex) in  settingData" :key="item.name">
        <td>{{ item.remark }}</td><td>{{ item.name }}</td><td>{{ item.conn }}</td><td>{{ item.table }}</td><td>{{ item.fsdType }}</td><td>{{ item.fields }}</td><td>{{ item.key }}</td>
        <td style="text-align: center">
          <a @click="openModel(item,0)">设置</a>
          <a class="ml-2" @click="openModel(item,1)">字段</a>
          <a class="ml-2" @click="restartOneIndex(item)">重启</a>
          <a class="ml-2" @click="deleteHandle(item,itemIndex)">删除</a>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
  <nodata v-else></nodata>
  <indexConfigModal :currentIndex="currentIndex" :dbInfo="dbInfo" v-if="isEdit && activeTab === 0" @close="closeModel" @save="saveHandle"></indexConfigModal>
  <indexFieldModal :currentIndex="currentIndex" :dbInfo="dbInfo"  v-if="isEdit && activeTab === 1"  @close="closeModel" @save="saveHandle"></indexFieldModal>
  <span class="tag is-danger is-light"> 注意：1:第一次设置索引需要打开"字段"设置并且保存一次. </span><br />
  <span class="tag is-warning is-light"> &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 2:需要搜索的、排序、分组操作的字段需要在字段设置里面做相应的设置,更多帮助<a @click="showHelp=true">点击这里</a> </span><br />
  <span class="tag is-warning is-light"> &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 3:对于中文超长字段(超过32765字节的)需要保存的字段,需要设置选择"存储"和"分词",建议使用ik分词器,否则会出现搜索结果不准确. </span><br />
  <span class="tag is-warning is-light"> &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 4:尽量排除一些Lob字段,譬如储存了文件照片等字段... </span><br />


  <help v-if="showHelp" @close="showHelp=false"></help>
</template>
<style scoped>
</style>
